/*
 * Created by JFormDesigner on Wed Mar 30 01:33:44 CST 2022
 */

package com.wangcanxuan.www.view.user;

import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.ValidateUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 找回密码界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class FindPasswordView extends JFrame {
    private final UserController userController;

    public FindPasswordView() {
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        initComponents();
        setVisible(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    private void back(ActionEvent e) {
        // TODO add your code here
        new LoginOnView();
        dispose();
    }

    private void findPassword(ActionEvent e) {
        // TODO add your code here
        User user = new User();
        String userName = userNameText.getText();
        String phone = phoneText.getText();
        String emailAddress = emailText.getText();
        String password = new String(passwordText.getPassword());
        String currentPassword = new String(currentPasswordText.getPassword());
        if (ValidateUtil.strIsNullOrEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(phone)) {
            JOptionPane.showMessageDialog(null, "手机号码不能为空！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(emailAddress)) {
            JOptionPane.showMessageDialog(null, "邮箱地址不能为空！");
            return;
        }
        if (ValidateUtil.isNotEmail(emailAddress)) {
            JOptionPane.showMessageDialog(null, "邮箱地址格式错误");
            return;
        }
        if (ValidateUtil.isNotMobile(phone)) {
            JOptionPane.showMessageDialog(null, "手机号码格式错误");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return;
        }
        if (ValidateUtil.isNotPassword(password)) {
            JOptionPane.showMessageDialog(null, "密码格式错误！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(currentPassword)) {
            JOptionPane.showMessageDialog(null, "确认密码不能为空！");
            return;
        }
        if (!password.equals(currentPassword)) {
            JOptionPane.showMessageDialog(null, "请确认密码是否一致！");
            return;
        }
        user.setUserName(userName);
        user.setPassword(password);
        user.setPhone(Long.valueOf(phone));
        user.setEmailAddress(emailAddress);
        ResultInfo<?> resultMessage = userController.findPassword(user);
        int code = resultMessage.getCode();
        String message = resultMessage.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new LoginOnView();
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            passwordText.setText("");
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        label2 = new JLabel();
        userNameText = new JTextField();
        label3 = new JLabel();
        phoneText = new JTextField();
        label4 = new JLabel();
        emailText = new JTextField();
        label5 = new JLabel();
        backButton = new JButton();
        label6 = new JLabel();
        findPasswordButton = new JButton();
        passwordText = new JPasswordField();
        currentPasswordText = new JPasswordField();

        //======== this ========
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u627e\u56de\u5bc6\u7801");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- label2 ----
        label2.setText("\u8981\u627e\u56de\u5bc6\u7801\u7684\u7528\u6237\u540d\uff1a");

        //---- label3 ----
        label3.setText("\u624b\u673a\u53f7\u7801\uff1a");

        //---- label4 ----
        label4.setText("\u90ae\u7bb1\u5730\u5740\uff1a");

        //---- label5 ----
        label5.setText("\u5bc6\u7801\uff1a");

        //---- backButton ----
        backButton.setText("\u8fd4\u56de\u767b\u5f55");
        backButton.addActionListener(e -> back(e));

        //---- label6 ----
        label6.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");

        //---- findPasswordButton ----
        findPasswordButton.setText("\u627e\u56de\u5bc6\u7801");
        findPasswordButton.addActionListener(e -> findPassword(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGap(22, 22, 22)
                                                                .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGap(40, 40, 40)
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(label5, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                                                        .addComponent(label4, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                                                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                                                                        .addComponent(label6, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))))
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(userNameText, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                        .addComponent(phoneText, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                        .addComponent(emailText, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                        .addComponent(passwordText, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                        .addComponent(currentPasswordText, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(103, 103, 103)
                                                .addComponent(findPasswordButton, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(84, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
                                .addGap(33, 33, 33)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                                        .addComponent(userNameText, GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                                        .addComponent(phoneText, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(label4, GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                                        .addComponent(emailText, GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(passwordText, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label6, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(currentPasswordText, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)
                                .addComponent(findPasswordButton, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(26, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JLabel label2;
    private JTextField userNameText;
    private JLabel label3;
    private JTextField phoneText;
    private JLabel label4;
    private JTextField emailText;
    private JLabel label5;
    private JButton backButton;
    private JLabel label6;
    private JButton findPasswordButton;
    private JPasswordField passwordText;
    private JPasswordField currentPasswordText;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
